import Base from "../base.js";
import cinemaService from "../../service/cinemaService.js";
export default class extends Base {
  constructor(props) {
    super(props);
    this.data = JSON.parse(localStorage.getItem("shu"));//获取到本地数据
  }
  render() {
    const template = `
    <form class="layui-form" action="" style="width:400px">
    <div class="layui-form-item">
      <label class="layui-form-label">影院名</label>
      <div class="layui-input-inline">
        <input type="text" required name="name" lay-verify="required" value="${this.data.name}" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">影院地址</label>
      <div class="layui-input-inline">
        <input type="text" name="address" required lay-verify="required" value="${this.data.address}" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">影院电话</label>
      <div class="layui-input-inline">
        <input type="text" name="phone" required lay-verify="required" value="${this.data.phone}" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
    <label class="layui-form-label">是否营业</label>
    <div class="layui-input-block check">
      <input type="radio" name="status" value="true" title="是">
      <input type="radio" name="status" value="false" title="否">
    </div>
  </div>
    <div class="layui-form-item">
      <div class="layui-input-block">
        <button type="button" class="layui-btn createBtn" lay-submit lay-filter="save">保存</button>
      </div>
    </div>
  </form>
    `
    this.$el.html(template);
    this.rerender();
  }
  async update(cinema) {
    const _id = this.data._id;
    await cinemaService.update(_id, cinema);
  }

  rerender(){
    if (this.data.status) {//获取input选择框的value值
      $(".check input:first-child").attr("checked",true);
    }else{
      $(".check input:last-child").attr("checked",true);
    }
  }

  handle() {
    this.form.on('submit(save)', (data) => {
      if(data.field.status=="true"){
        data.field.status = true;
      }
      if(data.field.status=="false"){
        data.field.status = false;
      }
      this.update(data.field);
      location.hash = "#/info/cinemaList";
      return false;
    })
  }

  layui() {
    this.form.render();
  }
}